import { createSlice,type PayloadAction } from '@reduxjs/toolkit'
import {setCompanyList,getCompanyList,removeCompanyList } from '../../utils/list'
import type{ localCompany }from '../../types'

// 定义状态接口（强烈推荐）
interface CompanyState {
  list: localCompany[] | null; // 根据实际需求调整类型，假设是字符串数组或null
}


const initialState:CompanyState = {
  list:getCompanyList() || null
}

const companySlice = createSlice({
  name: "company", //命名空间
  initialState, // ✅ 直接传入初始状态对象
  // 还原器
  reducers: {
    setList(state, action: PayloadAction<localCompany[]>) {
      state.list = action.payload;
      setCompanyList(action.payload);
    },
    removeList(state) {
      state.list = null; // 使用明确的空值而不是空字符串
      removeCompanyList();
    },
  },
});

// 提取 ActionCreators
export const { setList, removeList } = companySlice.actions;

// 默认导出 reducer（符合 RTK 标准实践）
const reducer = companySlice.reducer;
export default reducer;